import { SlickList, SlickItem } from 'vue-slicksort';

export default {
    components: {
        SlickItem,
        SlickList,
        qrcode: () => import('@/components/qrcodeReader/qrcodeReader')
    },
    data() {
        return {
            snapshotList: [
            {
                title: '故障',
                value: 0,
                tabTopId: 1,
                img: 'static/Images/snapshot_alarm.svg',
                name: 'home.Overview.FaultSnapshot',
                className: 'guzhang',
                path: 'Errors?title=故障'
            },
            {
                title: '警告',
                value: 0,
                tabTopId: 2,
                img: 'static/Images/snapshot_warning.svg',
                name: 'home.Overview.WarningSnapshot',
                className: 'jinggao',
                path: 'Warnings?title=警告'
            },
            {
                title: '信息',
                value: 0,
                tabTopId: 3,
                img: 'static/Images/snapshot_info.svg',
                name: 'home.Overview.InformationSnapshot',
                className: 'xinxi',
                path: 'Infos?title=信息'
            },
            {
                title: '设置',
                value: 0,
                tabTopId: 4,
                img: 'static/Images/snapshot_setup.svg',
                name: 'home.Overview.SetSnapshot',
                className: 'shezhi',
                path: 'Settings?title=设置'
            },
            {
                title: '资产',
                value: 0,
                tabTopId: 5,
                img: 'static/Images/snapshot_zichan.svg',
                name: 'home.Overview.AssetSnapshot',
                className: 'zichan',
                path: 'Assets?title=资产'
            }],

            dataItem: [],

            showModules: [],
            deleteModules: [],
            showEdit: false,
            qrcode: false,
            torchActive: false,
            camera: 'off',
            cameraTimer: null
        };
    },
    mounted() {
        if (localStorage.getItem('showModules')) {
            this.showModules = JSON.parse(localStorage.getItem('showModules'));
        }
        if (localStorage.getItem('deleteModules')) {
            this.deleteModules = JSON.parse(localStorage.getItem('deleteModules'));
        }
        this.getRealTimeEventTypeConfig();
        window.onRichScanCallback = this.onRichScanCallback;
    },
    beforeDestroy() {
        if (this.timeInterval) {
            clearInterval(this.timeInterval);
            this.timeInterval = null;
        }
    },

    methods: {
        editModules() {
            this.showEdit = true;
        },
        addModules(index) {
            this.showModules.push(this.deleteModules[index]);
            this.deleteModules = this.deleteModules.slice(0, index).concat(this.deleteModules.slice(index + 1));
        },
        removeModules(index) {
            this.deleteModules.push(this.showModules[index]);
            this.showModules = this.showModules.slice(0, index).concat(this.showModules.slice(index + 1));
        },
        saveModules() {
            this.showEdit = false;
            localStorage.setItem('showModules', JSON.stringify(this.showModules));
            localStorage.setItem('deleteModules', JSON.stringify(this.deleteModules));
            console.log(this.showModules);
        },
        sortResults(data) {
            console.log(data);
        },
        sortEnd() {
            console.log(arguments);
        },
        rightClick(data) {},

        // 获取实时快照类别配置
        getRealTimeEventTypeConfig() {
            this.$api
                .getRealTimeEventTypeConfig()
                .then(rt => {
                    if (rt.data.code === 200) {
                        let typeList = rt.data.data;
                        let numberStrAll = '';
                        for (let i = 0; i < typeList.length; i++) {
                            let btnValue = [];
                            for (
                                let j = typeList[i].snapshotLevelMin; j <= typeList[i].snapshotLevelMax; j++
                            ) {
                                btnValue += j + ',';
                            }
                            numberStrAll +=
                                btnValue.substring(0, btnValue.length - 1) +
                                ';';
                        }
                        let data = numberStrAll.substr(
                            0,
                            numberStrAll.length - 1
                        );
                        this.getRealTimeEventCounts(data);
                        this.timeInterval = setInterval(() => {
                            this.getRealTimeEventCounts(data);
                        }, 5000);
                    } else {
                        this.$message.error(rt.data.message);
                    }
                })
                .catch(err => {
                    console.log('err---', err);
                });
        },

        // 获取实时快照类别数量
        getRealTimeEventCounts(data) {
            this.$api
                .getRealTimeEventCount(data)
                .then(rt => {
                    if (rt.data.code === 200) {
                        let rtList = rt.data.data;
                        this.snapshotList.forEach((item, i) => {
                            if (rtList.length > 0) {
                                for (let j in rtList) {
                                    if (item.title === rtList[j].name) {
                                        item.value = rtList[j].value;
                                    }
                                }
                            }
                        });
                    } else {
                        this.$message.error(rt.data.message);
                    }
                })
                .catch(err => {
                    console.log('err---', err);
                });
        },

        goPage(type) {
            this.$router.push({ path: `/index/snapShots/${type}` });
        },

        RichScan() {

            // try { myJavaFun.RichScan(); } catch (e) {
            //             console.log(e);
            //         }
            if (document.location.protocol == 'http:') {
                try { myJavaFun.RichScan(); } catch (e) {
                    console.log(e);
                }
            } else {
                this.qrcode = true;
                this.camera = 'rear';

            }

            // this.qrcode = true;
            // this.camera = 'front';

        },
        onRichScanCallback(res) {
            this.qrcode = false;
            this.camera = 'off';
            this.$router.push({ path: '/index/scanResults', query: { assetInfo: res } });
        },

        changeQrcode(e) {
            this.qrcode = e;
        },
        changeCamera(e) {
            this.camera = e;
        }

        // 关闭相机
        //  turnCameraOff () {
        //     this.qrcode = false;
        //     this.camera = 'off';
        // },

        // 检查是否调用摄像头
        // async onInit (promise) {
        //     try {
        //         await promise;
        //     } catch (error) {
        //         // try { myJavaFun.RichScan(); } catch (e) {
        //         //     console.log(e);
        //         // }
        //         console.log(111,error)
        //         if (error.name === 'StreamApiNotSupportedError') {
        //         } else if (error.name === 'NotAllowedError') {
        //             this.errorMessage = 'Hey! I need access to your camera';
        //         } else if (error.name === 'NotFoundError') {
        //             this.errorMessage = 'Do you even have a camera on your device?';
        //         } else if (error.name === 'NotSupportedError') {
        //             this.errorMessage =
        //                 'Seems like this page is served in non-secure context (HTTPS, localhost or file://)';
        //         } else if (error.name === 'NotReadableError') {
        //             this.errorMessage =
        //                 "Couldn't access your camera. Is it already in use?";
        //         } else if (error.name === 'OverconstrainedError') {
        //             this.errorMessage =
        //                 "Constraints don't match any installed camera. Did you asked for the front camera although there is none?";
        //         } else {
        //             this.errorMessage = 'UNKNOWN ERROR: ' + error.message;
        //         }
        //     }

        // }

    }
};